package solution

import "math/rand"

// https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xn6gq1/
// 设计题目：打乱数组
// 算法: 递归遍历
// 时间复杂度: O(n)
type Solution struct {
	originArr []int
}

// name: ArrConstructor
func ArrConstructor(nums []int) Solution {
	s := Solution{
		originArr: nums,
	}
	return s
}

/** Resets the array to its original configuration and return it. */
func (this *Solution) Reset() []int {
	return this.originArr
}

/** Returns a random shuffling of the array. */
func (this *Solution) Shuffle() []int {
	// 生成随机数作为下标，并组装生成数组
	copyArr := make([]int, len(this.originArr))
	copy(copyArr, this.originArr)
	// 每个数都随机和一个数进行交换
	for index := 0; index < len(copyArr); index++ {
		toSwapIndex := int(rand.Float32() * float32(len(copyArr)))
		copyArr[index], copyArr[toSwapIndex] = copyArr[toSwapIndex], copyArr[index]
	}
	return copyArr
}

/**
 * Your Solution object will be instantiated and called as such:
 * obj := Constructor(nums);
 * param_1 := obj.Reset();
 * param_2 := obj.Shuffle();
 */
